//
//  CCKSunCategoryController.m
//  budejieMe
//
//  Created by SuperJJ on 16/8/28.
//  Copyright © 2016年 SuperJJ. All rights reserved.
//

#import "CCKSubMainController.h"
#import "CCKSubscribeModel.h"
#import "UINavigationItem+ItemMargin.h"
#import "CCKSubMainHeadCell.h"
#import "CCKSubMainHeadModel.h"

static NSString * const kCellRidHead = @"kCellRidHead";

@interface CCKSubMainController ()

@property (nonatomic, strong) CCKSubMainHeadModel * headModel;

@end

@implementation CCKSubMainController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupNavItem];
    [self initTableView];
    [self downloadData];
}

- (void)downloadData
{
    [CCKSubMainHeadModel downloadDataModelWithThemeId:[self.subscribeModel.theme_id integerValue] andSuccessBlock:^(CCKSubMainHeadModel *model)
     {
         _headModel = model;
         
         [self.tableView reloadData];
         
     } andFailedBlock:^(NSError *error)
     {
         NSLog(@"CCKSubMainHeadModel Download Error : %@",error);
     }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupNavItem
{
    self.navigationItem.title = self.subscribeModel.theme_name;
    
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    
    UIButton *btnSubscribe = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [btnSubscribe setBackgroundImage:[UIImage imageNamed:@"tag_subscribe_tag_bg"] forState:UIControlStateNormal];
    [btnSubscribe setBackgroundImage:[UIImage imageNamed:@"tag_subscribe_tag_bgN"] forState:UIControlStateHighlighted];
    
    [btnSubscribe setTitle:@"+ 订阅" forState:UIControlStateNormal];
    
    btnSubscribe.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [btnSubscribe sizeToFit];
    
    btnSubscribe.x = btnSubscribe.x + 10;
    
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:btnSubscribe] andMargin:12];
}

- (void)initTableView
{
    self.tableView.estimatedRowHeight = 200;
    self.tableView.rowHeight = UITableViewAutomaticDimension;

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerClass:[CCKSubMainHeadCell class] forCellReuseIdentifier:kCellRidHead];
}


#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    CCKSubMainHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellRidHead forIndexPath:indexPath];
    
    if (_headModel)
    {
        cell.model = _headModel;
    }
    
    return cell;
}


/*1
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
